﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Entity;
using HanGu.Services;
using HanGu.Common;

namespace Hangu.WebClient.Controllers.Admin
{
    public class ManagerController : AdminBaseController
    {
        private IManager managerServices;

        public ManagerController()
        {
            navTabId = "Manager";
            this.managerServices = ServiceFactory.GetService<IManager>();
        }
       
        //
        // GET: /Manager/

        public ActionResult Index()
        {
            //ViewData["RowCount"] = 10;
            List<Manager> managerList = managerServices.GetAll();
            return View("~/Views/Admin/Manager/index.cshtml", managerList);
        }

        //
        // GET: /Manager/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Manager/Create

        public ActionResult Create(int id)
        {
            Manager manager = new Manager();
            if (id != 0)
            {
                manager = managerServices.GetByID(id);
            }
            return View("~/Views/Admin/Manager/Create.cshtml", manager);
        }

        //
        // POST: /Manager/Create

        [HttpPost]
        public string Create(FormCollection collection)
        {
            try
            {
                Manager model=new Manager();
                TryUpdateModel(model,collection);
                var tempManager = managerServices.GetByUser(model.User);
                if (tempManager != null)
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.UserExitsed, navTabId, "", "");
                    return "{" + ajaxStr + "}";
                }
                model.Creator = CurentUser.UserName;
                model.CreateTime = DateTime.Now;
                if (ModelState.IsValid)
                {
                    if (model.ID > 0)
                    {
                        if (Request["setpwd"] != null)//重设密码
                        { model.Pwd = Helper.MD5(model.Pwd); }
                        this.managerServices.Update(model);
                    }
                    else
                    {
                        model.Pwd = Helper.MD5(model.Pwd);
                        this.managerServices.Add(model);
                    }
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");

                }
                else
                { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); ; }
            }
            catch(Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); 
            }
            return "{"+ajaxStr+"}";
        }

        //
        // GET: /Manager/Edit/5

        public ActionResult ReSetPwd(int id)
        {
            Manager manager = new Manager();
            if (id != 0)
            {
                manager = managerServices.GetByID(id);
            }
            return View("~/Views/Admin/Manager/ReSetPwd.cshtml", manager);
        }

        //
        // POST: /Manager/Edit/5

        [HttpPost]
        public ActionResult ReSetPwd(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Manager/Delete/5

        [HttpGet]
        public string Delete(int id)
        {
            try
            {
                managerServices.Delete(id);
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{"+ajaxStr+"}";
        }

        //
        // POST: /Manager/Delete/5

        [HttpPost]
        public string Delete(int id, FormCollection collection)
        {
            try
            {
                managerServices.Delete(id);
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";
        }
    }
}
